<?php
error_reporting(0);
session_start();

include_once "src/controller/blmanagement/ReservationManagement.php";
include_once "src/controller/blmanagement/ResourceManagement.php";
include_once "src/controller/blmanagement/UserAccountManagement.php";
include_once "src/model/Reservation.php";
include_once "src/model/Resource.php";
include_once "src/model/User.php";
include_once "src/misc/ClientReponseGenerator.php";

try {

    $user = UserAccountManagement::getUserByEmail($_SESSION["email"]);

    $reservations = ReservationManagement::findAllReservationsByResourceId($_SESSION["resourceIdForReservationCalendar"]);

    $items = "[";
    $reservationsCount = count($reservations);
    for ($i = 0; $i < $reservationsCount; $i++) {
        $reservation = $reservations[$i];
        $resource = ResourceManagement::getResourceById($reservation->getResourceID());
        if (($reservation->getReOccurrenceID() == 1) || ($reservation->getReOccurrenceID() == 2) || ($reservation->getReOccurrenceID() == 3)) {

            $reoccurrenceIncerment = "";
            if ($reservation->getReOccurrenceID() == 1) {
                $reoccurrenceIncerment = " +1 day";
            } else if ($reservation->getReOccurrenceID() == 2) {
                $reoccurrenceIncerment = " +1 week";
            } else {
               $reoccurrenceIncerment = " +1 month";
            }

            $tempStartDate = $reservation->getStartDate();
            $tempStopDate = $reservation->getStopDate();
            
            for ($j = strtotime($tempStartDate); $j <= strtotime($tempStopDate); $j = strtotime(date("Y-m-d", $j) . $reoccurrenceIncerment)) {
                if ($user->getUserId() == $reservation->getUserID()) {
                    $items .= '{"id":'.$reservation->getReservationID().', "title":"Reserved","start":"'.date("Y-m-d", $j).'","end":"'.date("Y-m-d", $j).'"}';
                } else {
                    $items .= '{"id":'.$reservation->getReservationID().', "title":"Reserved","start":"'.date("Y-m-d", $j).'","end":"'.date("Y-m-d", $j).'","className":"disableReservation"}';
                }
                if ((strtotime(date("Y-m-d", $j) . $reoccurrenceIncerment)) <= strtotime($tempStopDate)) {
                    $items .= ",";
                } else {
                    if (($reservationsCount > 1) && (($i + 1) < $reservationsCount)) {
                        $items .= ",";
                    }
                }
            }
        } else {
            if ($user->getUserId() == $reservation->getUserID()) {
                $items .= '{"id":'.$reservation->getReservationID().', "title":"Reserved","start":"'.$reservation->getStartDate().'","end":"'.$reservation->getEndDate().'"}';
            } else {
                $items .= '{"id":'.$reservation->getReservationID().', "title":"Reserved","start":"'.$reservation->getStartDate().'","end":"'.$reservation->getEndDate().'","className":"disableReservation"}';
            }
            if (($reservationsCount > 1) && (($i + 1) < $reservationsCount)) {
                $items .= ",";
            }
        }
    }
    $items .= "]";
    echo $items;

} catch (Exception $e) {
    echo ClientReponseGenerator::simpleJsonResponse(0, "LoadReservationCalendarDataException", "");
}

?>

